package pe.hongi.frist;





import android.app.Activity;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class mediaplayer extends Activity
{   
	
	
	VideoView mVideoView;
	
	String url_txt, szPlayMovie;
	
	MyDeskDetail_DBAdapter detail;
	
	 SurfaceHolder mHolder;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		 setContentView(R.layout.mediaplayer);
		 
	  detail=new MyDeskDetail_DBAdapter(this);
	  detail.open();
		 
		Bundle extras = getIntent().getExtras();         
	    if(extras!=null){
	    	if(extras.getString("media_url")!=null){
	    		         
	    	 szPlayMovie=extras.getString("media_url");
	    		
	    	}
	    }
		
	    if(szPlayMovie.length() ==0){
	    	
	    	Toast.makeText(this, "지원되지 않는 동영상 포맷 입니다.", 1).show();
			finish();
	    	return;
	    }
	    
	    
	    try
		{
	    
		Uri uri = Uri.parse(szPlayMovie);
		
		
		url_txt=""+uri;
		
		mVideoView = (VideoView)findViewById(R.id.videoview);
		
		MediaController mediaCtrl = new MediaController(this);
		mediaCtrl.setAnchorView(mVideoView);
		mVideoView.setMediaController(mediaCtrl);
		mVideoView.setVideoURI(uri);
		mVideoView.requestFocus();
		
		
		
//		mVideoView.start();
		
		
		if(detail.countEntry_for_MyDeskDetail_url(""+url_txt)==0){
			
			mVideoView.start();
		
		}else{
			
			Cursor cur = detail.getEntry_for_MyDeskDetail_url(""+url_txt);
			String MyDeskDetail_time_num_txt=cur.getString(cur.getColumnIndexOrThrow("MyDeskDetail_time_num"));
			int MyDeskDetail_time_num_int=Integer.parseInt(MyDeskDetail_time_num_txt);
			
				Log.d("ge","MyDeskDetail_time_num_int"+MyDeskDetail_time_num_int);
				
		 		mVideoView.seekTo(MyDeskDetail_time_num_int);
		 		mVideoView.start();
		 		cur.close();
			
		}
		
		mVideoView.setOnCompletionListener(new OnCompletionListener()
		{
			public void onCompletion(MediaPlayer arg0)
			{
				mVideoView.pause();
				Toast.makeText(mediaplayer.this, "종료", 1).show();
				mVideoView.stopPlayback();
		     	}
		     });
		
		}catch (Exception e){
			
			e.printStackTrace();
			finish();
		}
		
		
	}
	
	
	  @Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
//	   detail.close();	
		
	}


	@Override
	   public boolean onKeyDown(int keyCode, KeyEvent event) {
	    // TODO Auto-generated method stub
		   
  
		   switch (keyCode) {
	    
	      case KeyEvent.KEYCODE_BACK:{
	    	  
				if(detail.countEntry_for_MyDeskDetail_url(""+url_txt)==0){
					
					int current_int = mVideoView.getCurrentPosition();
					
					int all_int = mVideoView.getDuration();
					
					int off_int=((current_int*60)/all_int);
					
					if(off_int>60) off_int=60;
					
					if(off_int>55){
					
					detail.insertEntry(url_txt, ""+all_int, ""+0, ""+off_int);
						
					}else{
					
					detail.insertEntry(url_txt, ""+all_int, ""+current_int, ""+off_int);
					
					}
					
				}else{
					
					Cursor cur = detail.getEntry_for_MyDeskDetail_url(""+url_txt);
					
					long id_txt=cur.getLong(cur.getColumnIndexOrThrow("id"));
					String MyDeskDetail_complete=cur.getString(cur.getColumnIndexOrThrow("MyDeskDetail_complete"));
					
					cur.close();
					int complete_int=Integer.parseInt(MyDeskDetail_complete);
					
                    int current_int = mVideoView.getCurrentPosition();
					
                    int all_int = mVideoView.getDuration();
					
                    int off_int=((current_int*60)/all_int);
					
					
					Log.d("ge",off_int+"="+ ((current_int*100)/all_int));
					
					
					if(complete_int>=off_int){
						
						 if(complete_int>60) complete_int=60;
						 if(off_int>55) current_int=0;
						 
					detail.updateEntry(id_txt, url_txt, ""+all_int, ""+current_int,""+complete_int);
						
					}else{
						
						 if(off_int>60) off_int=60;
						 if(off_int>55) current_int=0;
						 
					detail.updateEntry(id_txt, url_txt, ""+all_int, ""+current_int,""+off_int);	
						
					}
				}
				
				detail.close();
	    	    mVideoView.stopPlayback();
	    	    mVideoView.invalidate();
	    	    mVideoView=null;
				finish();
	    	
	           }
	       }
		return false;	

	   }
}
